package com.zheng.hr.rpc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;

import com.zheng.common.util.WeixinUtil;
import com.zheng.hr.dao.model.HrCheckin;
import com.zheng.hr.dao.mapper.HrCheckinMapper;
import com.zheng.hr.rpc.api.HrApprovalService;
import com.zheng.hr.rpc.api.HrCheckinService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.oa.common.utils.TimeStampUtil;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-04-28
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class HrCheckinServiceImpl extends BaseServiceImpl<HrCheckinMapper, HrCheckin> implements HrCheckinService {
    private final static Logger _log = LoggerFactory.getLogger(HrCheckinServiceImpl.class);
    @Autowired
    private UpmsUserService userService;
    @Autowired
    private HrApprovalService approvalService;

    /**
     * 添加签到记录
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public boolean insertCheckinArray(long beginTime, long endTime) {
        //获取所有需要的签到用户
        List<UpmsUser> users = userService.selectList(new EntityWrapper<UpmsUser>().eq("real_user", UpmsUser.IS_REAL_USER));
        List<String> usernameList = new ArrayList<>();
        for (UpmsUser user : users) {
            usernameList.add(user.getUsername());
        }
        //获取数据
        JSONArray checkinArray = WeixinUtil.getCheckInData(beginTime, endTime, usernameList);
        List<HrCheckin> checkins = new ArrayList<>();
        //取出其他数据
        for (int i = 0, length = checkinArray.size(); i < length; i++) {
            JSONObject checkInObject = checkinArray.getJSONObject(i);
            HrCheckin checkin = checkInObject.toJavaObject(HrCheckin.class);
            checkin.setCheckinDate(new Date(checkin.getCheckinTime() * 1000));
            checkin.setCheckinTime(TimeStampUtil.timeStampChange(checkin.getCheckinTime()));
            checkin.setUserName(checkInObject.getString("userid"));
            //去重
            boolean isRepeat = false;
            for (HrCheckin ck : checkins) {
                if (ck.getCheckinTime().equals(checkin.getCheckinTime()) && ck.getUserName().equals(checkin.getUserName())) {
                    isRepeat = true;
                    break;
                }
            }
            if (!isRepeat) {
                checkins.add(checkin);
            }
        }
        if (checkins.size() < 1) {
            return false;
        }
        return insertBatch(checkins);
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void scheduler() {
        _log.info("生成每日考勤、审批记录！");
        Calendar calendar = Calendar.getInstance();
        long endTime = calendar.getTimeInMillis() / 1000;
        long startTime = endTime - (60 * 60 * 24);
        insertCheckinArray(startTime, endTime);
    }

}
